<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary">添加权限</el-button>
      <el-table default-expand-all :data="list" row-key="id">
        <el-table-column label="名称" prop="name" />
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button v-if="row.type === 1" size="mini" type="text"
            @click="addPermission(row.type,row.id)">添加</el-button>
            <el-button size="mini" type="text"
            @click="editPermission(row.id)"
            >编辑</el-button>
            <el-button size="mini" type="text"
             @click="delPermission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog width="500px" :visible.sync="showDialog" @close="btnCancel">
      <!-- 表单内容 -->
      <el-form
        label-width="120px"
        ref="permissionForm"
        :model="permissionForm"
        :rules="rules"
        :title="showText"
      >
        <el-form-item label="权限名称" prop="name">
          <el-input
            v-model="permissionForm.name"
            style="width: 300px"
            size="mini"
          />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input
            style="width: 300px"
            size="mini"
            v-model="permissionForm.code"
          />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input
            style="width: 300px"
            size="mini"
            v-model="permissionForm.description"
          />
          </el-form-item>
          <el-form-item label="启用" prop="state">
            <el-switch
              size="mini"
              v-model="permissionForm.state"
              :active-value="1"
              :inactive-value="0"
            />
          </el-form-item>
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" size="mini" @click="btnOk"
                >确定</el-button
              >
              <el-button size="mini" @click="btnCancel">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import {
  getPermissionList,
  getPermissionDetail,
  addPermission,
  updatePermission,
  delPermission
} from "@/api/permission";
import { transListToTreeData } from "@/utils";
export default {
  name: "Permission",
  data() {
    return {
      list: [],
      showDialog: false,
      permissionForm: {
        id: "",
        name: "",
        code: "",
        state: "",
        description: "",
        type: 1,
        pid: "",
      },
      rules: {
        name: [
          {
            required: true,
            message: "权限名称不能为空",
            trigger: "blur",
          },
        ],
        description: [
          {
            required: true,
            message: "权限描述不能为空",
            trigger: "blur",
          },
        ],
        code: [
          {
            required: true,
            message: "权限标识不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getPermissionList();
  },
  methods: {
    async getPermissionList() {
      this.list = transListToTreeData(await getPermissionList(), 0);
    },
    btnOk() {
      this.$refs.permissionForm.validate(async (isOk) => {
        if (isOk) {
          let msg = "新增";
          if (this.permissionForm.id) {
            msg = "更新";
            updatePermission(this.permissionForm);
          } else {
            addPermission(this.permissionForm);
          }
          //提示消息
          this.$message.success(`${msg}部门成功`);
          //关闭
          this.cancelDept();
        }
      });
    },
    computed: {
      showText() {
        return this.formData.id ? "编辑" : "新增";
      },
    },
    btnCancel() {
      this.$refs.permissionForm.resetFields();
      this.showDialog = false;
    },
    addPermission(type, pid) {
      this.permissionForm.type = type;
      this.permissionForm.pid = pid;
      this.showDialog = false;
    },
    async editPermission(id) {
      // 根据获取id获取详情
      this.formData = await getPermissionDetail(id);
      this.showDialog = true;
    },
    async delPermission(id) {
      //删除部门
      this.$confirm("请确认是否要删除").then(async () => {
        await delPermission(id);
        this.$message.success("删除权限成功");
        this.getPermissionList();
      });
    },
  },
};
</script>
<style>
.btn-add {
  margin: 10px;
}
</style>